// pages/server_end/message_list/message_list.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    type:0,
    page: 1,
    list:[],
    noOrder:0,
    hasMoreData:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type:options.type,
      index:options.type
   })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          that.setRead();
          that.getMessage();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getMessage();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.hasMoreData) {
      this.getMessage();
   } 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getMessage() {
    var that = this;
    let param = {
        type:that.data.type,
        page:that.data.page,
        token:that.data.token
    }
    wx.showNavigationBarLoading();  //在当前页面显示导航条加载动画
    request.request(url.getmessagelist, param, url.REQUEST_METHOD_GET).then((res) => {
          wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
          if (that.data.page == 1) {
              that.setData({
                list:[],
                noOrder:1,
              })
          }
          var contentlistTem = that.data.list;
            if (res.data.data.data.length > 0) {
              var contentlist = res.data.data.data;
              if (contentlist.length < 10) {
                that.setData({
                  list: contentlistTem.concat(contentlist),
                  hasMoreData: false,
                  noOrder:0,
                })
              } else {
                that.setData({
                  list: contentlistTem.concat(contentlist),
                  hasMoreData: true,
                  page: that.data.page + 1,
                  noOrder:0,
                })
              }
            }else{
               that.setData({
                hasMoreData: false
               })
            }
    })

  },
  setRead(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      type:that.data.type
    }
    request.request(url.messagesetread, param, url.REQUEST_METHOD_GET).then((res) => {
      console.log(res);
       
    })   
  },
  goOrder(e){
    var id =e.currentTarget.dataset.id;
    if(this.data.type!=3){
      wx.navigateTo({
        url:'../order/order?id='+id
      })						
    }else{
      wx.navigateTo({
        url:'../server_comment/server_comment?id='+id
      })						
    }
 },
 callPhone(e){
   var phone =e.currentTarget.dataset.phone;
   wx.makePhoneCall({
     phoneNumber: phone 
   })
 }
})